package com.winai.launcher.view.timeManager;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.base.BaseRecAdapter;
import com.winai.launcher.base.BaseRecViewHolder;
import com.winai.launcher.bean.TabBean;
import com.winai.launcher.common.GlobalPublisher;
import com.winai.launcher.common.SignalSlot;
import com.winai.launcher.override.CustomScrollViewPager;
import com.winai.launcher.override.LazyFragmentPagerAdapter;
import com.winai.launcher.utils.PicassoLoadUtils;
import com.winai.launcher.view.fragment.AlarmClockFragment;
import com.winai.launcher.view.fragment.CalendarFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.winai.launcher.common.GlobalPublisher.OPEN_ALARM;
import static com.winai.launcher.common.GlobalPublisher.OPEN_CALENDAR;

/**
 * 作者：康少
 * 时间：2019/12/12 0012
 * 说明：闹钟
 */
public class TimeManagerActivity extends BaseActivity {
    private Context mContext = this;

    @BindView(R.id.viewPager)
    CustomScrollViewPager viewPager;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;

    private TabAdapter tabAdapter;
    private List<TabBean> tabBeanList = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int selectPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_manager);
        ButterKnife.bind(this);
        start();
    }

    private void start() {
        initObject();
        initView();
        initListener();
    }

    private void initObject() {
        selectPage = getIntent().getIntExtra("selectPage", 0);
        tabAdapter = new TabAdapter();
        LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(tabAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.setLoadMoreEnabled(false);

        AlarmClockFragment alarmClockFragment = new AlarmClockFragment();
        CalendarFragment calendarFragment = new CalendarFragment();
        mFragments.add(alarmClockFragment);
        mFragments.add(calendarFragment);

        viewPager.setScrollable(false);
        viewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager()));
        if (selectPage > 1) {
            selectPage = 1;
        }
        viewPager.setCurrentItem(selectPage);
    }

    private void initView() {
        tabBeanList.clear();
        tabBeanList.add(new TabBean(1, selectPage == 0));
        tabBeanList.add(new TabBean(2, selectPage == 1));
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                tabAdapter.refreshData(tabBeanList);
            }
        });
    }

    private void initListener() {
        GlobalPublisher.setListener(GlobalPublisher.ANY, new SignalSlot.Slot() {
            @Override
            public void onSignal(int what, int arg1, int agr2, Object argObj) {
                switch (what) {
                    case OPEN_CALENDAR:
                        toggleViewPage(2);
                        tabAdapter.getData().get(0).setChecked(false);
                        tabAdapter.getData().get(1).setChecked(true);
                        //更新视图
                        tabAdapter.notifyDataSetChanged();
                        break;
                    case OPEN_ALARM:
                        toggleViewPage(1);
                        tabAdapter.getData().get(0).setChecked(true);
                        tabAdapter.getData().get(1).setChecked(false);
                        //更新视图
                        tabAdapter.notifyDataSetChanged();
                        break;
                }
            }
        });
    }

    /**
     * 说明：切换ViewPager，id ,1：显示闹钟；2：显示日历
     */
    private void toggleViewPage(int id) {
        switch (id) {
            case 1:
                viewPager.setCurrentItem(0);
                break;
            case 2:
                viewPager.setCurrentItem(1);
                break;
        }
    }

    private class TabPagerAdapter extends LazyFragmentPagerAdapter {
        public TabPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "";
        }

        @Override
        protected Fragment getItem(ViewGroup container, int position) {
            return mFragments.get(position);
        }
    }

    /**
     * 作者：康少
     * 时间：2019/12/12 0012
     * 说明：tab适配器
     */
    private class TabAdapter extends BaseRecAdapter<TabBean, TabAdapter.ViewHolder> {
        @Override
        public void onHolder(ViewHolder holder, TabBean bean, int position) {
            RelativeLayout clItem = holder.getView(R.id.cl_item);
            ImageView ivTab = holder.getView(R.id.iv_tab);
            if (bean.isChecked()) {
                if (bean.getId() == 1) {
                    PicassoLoadUtils.getInstance().picassoLoadNoPlaceholder(R.mipmap.tab_time_checked, ivTab);
                } else if (bean.getId() == 2) {
                    PicassoLoadUtils.getInstance().picassoLoadNoPlaceholder(R.mipmap.tab_calendar_checked, ivTab);
                }
                clItem.setBackgroundColor(Color.parseColor("#2f2f31"));
            } else {
                if (bean.getId() == 1) {
                    PicassoLoadUtils.getInstance().picassoLoadNoPlaceholder(R.mipmap.tab_time_unchecked, ivTab);
                } else if (bean.getId() == 2) {
                    PicassoLoadUtils.getInstance().picassoLoadNoPlaceholder(R.mipmap.tab_calendar_unchecked, ivTab);
                }
                clItem.setBackgroundColor(Color.parseColor("#121214"));
            }
            clItem.setOnClickListener(v -> {
                if (!bean.isChecked()) {
                    toggleViewPage(bean.getId());
                    for (TabBean bb : getData()) {
                        bb.setChecked(false);
                    }
                    bean.setChecked(true);
                    //更新视图
                    notifyDataSetChanged();
                }
            });
        }

        @Override
        public ViewHolder onCreateHolder() {
            return new ViewHolder(getViewByRes(R.layout.item_tab));
        }

        private class ViewHolder extends BaseRecViewHolder {
            ViewHolder(View itemView) {
                super(itemView);
            }
        }
    }
}
